import csv
import json

def convert_emb_to_csv(emb_file, csv_file):
    """将嵌入文件转换为CSV格式"""

    with open(emb_file, 'r') as f, open(csv_file, 'w', newline='') as out_f:
     
        writer = csv.writer(out_f)
        header = f.readline().strip().split()
        num_nodes, dim = int(header[0]), int(header[1])
        writer.writerow(["id:ID", "embedding:STRING"])

        for line in f:
         
            parts = line.strip().split()
            node_id = parts[0]
            embedding = json.dumps([float(x) for x in parts[1:]]) 
            writer.writerow([node_id, embedding])

graph_name = "Yelp"

emb_file = f"./{graph_name}/{graph_name}_Ours.emb"
# emb_file = f"./{graph_name}/{graph_name}_Lap.emb"
# emb_file = f"./{graph_name}/{graph_name}_GCN.emb"
# emb_file = f"./{graph_name}/{graph_name}_DeepWalk.emb"

csv_file = f"./{graph_name}/{graph_name}_Ours_embeddings.csv"
# csv_file = f"./{graph_name}/{graph_name}_Lap_embeddings.csv"
# csv_file = f"./{graph_name}/{graph_name}_GCN_embeddings.csv"
# csv_file = f"./{graph_name}/{graph_name}_DeepWalk_embeddings.csv"

convert_emb_to_csv(emb_file, csv_file)

print(f"============数据集{graph_name}的emb文件转换完成,其CSV格式文件生成完毕============")
